perm filename BELTEX.SAI[TEX,DEK] blob sn#362427 filedate 1978-06-20 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00008 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	begin comment convert Belfast format to approximate-TEX
C00005 00003	while true do begin label mainloop,eop
C00007 00004	decoding tables
C00011 00005	i/o procedures
C00017 00006	individual processing procedures
C00021 00007	the main control logic
C00027 00008	ending it
C00028 ENDMK
C⊗;
begin comment convert Belfast format to approximate-TEX;
external procedure bail;
comment First form a file a file containing one or more original paper
tape contents, thus:
.copy/q g←/ascii ptr:
(repeating this via ↑<CR> if necessary, with fingers crossed, until the
tape has been successfully read in)
.copy acc←acc,g.asc (simply acc←g the first time)
.ex belfst
file=acc
(ACC.IN should contain an ascii equivalent of the paper tape contents read into acc)
Then use this program to convert the file further, after which you should
go through by hand and check everything carefully.
Recommended way to check everything: First go through and put in "sectionbegin"s
and "exbegins" etc., then systematically look at all $ signs to fix
displayed formulas mostly, then check \sl to make sure formulas aren't embedded,
then look for any | signs standing for things I didn't bother to convert,
then remove ?? signs standing for things this program didn't know, then
look for idioms dealing with MIX, exercises part(a), System/360,
random-number, !'s, less vs. fewer, modulo, etc.,
then go through once again to insert triangles on exercises and \yskips at
implicit subsections;
boolean anspgs;
integer eof,chan,flag,brch;
string ff,fn,buf,crlf,stmp;
setbreak(1,'14&(crlf←'15&'12),null,"IS");
setbreak(2,ff←'14,null,"IS");
while true do begin label quit;
eof←0;
outstr("text_file = "); fn←inchwl;
open(chan←getchan,"dsk",0,2,0,130,brch,eof);
lookup(chan,fn&".in",flag);
setprint(fn&".out","B");
brch←0; while brch≠'14 do buf←input(chan,2);
outstr("Are these answer pages? "); stmp←inchwl;
if stmp="y" or stmp="Y" then anspgs←true else anspgs←false;
while true do begin label mainloop,eop;
comment the following to be done for every page;
integer f,r,k,lastchar,italmode,limmode,x,y,cfont,basemode,italsave;
integer array char[0:99]; comment the input queue of characters read so far;
string temp,basesave;
boolean fracmode,mjustmode,exmode,boldmode;
comment decoding tables;
preload_with [40]"??",
"??","??","??","??",",","??",":","??",
"0","1","2","3","4","5","6","7",
"8","9",[6]"??",
"??","\in","??","\dprime ","\rslash ","≤","\parallel ","|",
"","??","\int ","\sum ","←","→","\pm ","\cdot ",
"??",">","≥","↑\prime ","\{","??","\approx ","<",
"∞","\}","~",[5]"??",
"??","A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W",
"X","Y","Z",[5]"??";
own string array table1[0:127];

preload_with [16]"??",
"??",";","??","??","??","??","\times","??",
"-",[7]"??",
"??","??","??","(","=","+",")","\varphi ",
"\Xi ","\Gamma ","*","+","??","\Delta ","??","??",
[8]"??",
"??","??","\sum ","\varsigma ","\Psi ","=","??","\prod ",
[24]"??",
"??","??","??","\Omega ","\Theta ","??","??","??",
"\Phi ","α","β","\psi ","\delta ","ε","\phi ","\gamma ",
"\eta ","\iota ","\xi ","\kappa ","λ","\mu ","\nu ","\cdot ",
"π","\vartheta ","\rho ","\sigma ","\tau ","\theta ","\omega ","??",
"\chi ","\upsilon ","\zeta ",[5]"??";
own string array table2[0:127];

preload_with [48]"??",
"↑\prime",[7]"??",
[8]"??",
"??","\left\{","\ominus ","5","\left\langle","\mathop{∩}",
	"}","|bigcard ",
"","∨","??","","3","4","\otimes ","1",
"2","∩","∨","}","⊂","\subset ","\oplus ","∩",
"\hat ","\subset ",[6]"??",
"??","\left(","\langle ","0","\left[","\mathop{∪}","}","|bigabs ",
"\left/","∧","\int ","\sum ","8","9","\odiv ","6",
"7","∪","∨","}","⊃","\superset ","\rangle ","∪",
"\hat ","\superset ",[6]"??";
own string array table3[0:127];

preload_with [48]"??",
[7]"??","∧",
"∨",[7]"??",
"??","∨","\S ","\bfslash ",[4]"??",
[4]"??","\rfloor ","\dblrtarrow ","\M ","\nelt ",
"\rceil ","\oless ",[4]"??","\R ","??",
[8]"??",
[3]"??","\mp ",[4]"??",
"??","∧","\munion ","\mp ","\lfloor ","↔","\oast ","≡",
"\lceil ","\ogrtr ",[6]"??",
[8]"??";
own string array table4[0:127];
comment i/o procedures;
integer procedure nchar;
begin comment get next Belfast character from file;
while buf=0 do
	begin buf←input(chan,1);
	if buf then buf←buf[8 to ∞];
	if eof or (brch='14) then return('14);
	end;
return(lop(buf));
end;

integer procedure nexchar;
comment get next Belfast character from internal queue;
if f=r then return(nchar) else
	begin if(f←f+1)≥100 then f←0;
	return(char[f]);
	end;

boolean procedure try(string s);
begin comment if s is next in input, erase it and return true,
else erase nothing and return false;
boolean b; integer i;
b←true; i←f;
while (s≠0) and b do
	begin if i=r then
		begin if(r←r+1)≥100 then r←0;
		char[r]←nchar;
		end;
	if(i←i+1)≥100 then i←0;
	if lop(s)≠char[i] then b←false;
	end;
if b then f←i;
return(b);
end;

integer procedure snexchar;
begin integer t,u;
if try("α") then begin t←table2[nexchar];u←nexchar end
else if try ("|¬") then t←table1[nexchar] else t←nexchar;
return(t)
end;

procedure nop; go to mainloop;

procedure newline;
begin print(crlf); k←0;
end;

procedure output(string s);
if s=0 then nop else
begin
if (cfont≠0) and (s≠" ") then
	begin print("}"); k←k+1; cfont←0;
	end;
if basemode≠0 then
	begin if length(basesave)=1 then begin print(basesave);k←k+1 end else
		begin print("{",basesave,"}");k←k+2+length(basesave) end;
	basemode←basesave←0;
	end
else if lastchar=" " then
	begin integer x; while s=" " do x←lop(s);
	if s=0 then nop;
	if k≥60 then newline else print(" ");
	end;
lastchar←s[∞ for 1];
k←k+length(s);
if lastchar=" " then print(s[1 to ∞-1]) else print(s);
go to mainloop;
end;

procedure font(string s);
begin
if lastchar = " " then
	begin if k≥60 then newline else print(" "); lastchar←0;
	end;
if cfont=s then
	begin print(temp←nexchar);k←k+1;
	go to mainloop;
	end;
if italmode = 0 and cfont = 0 then
	begin print("{");k←k+1 end;
italmode←0; 
cfont←s;
if s="=" then print("\bf ",temp←nexchar) else
print("\:",s," ",temp←nexchar); k←k+5;
go to mainloop;
end;

procedure baseshift(integer d);
begin if lastchar = " " then
	begin if k≥60 then newline else print(" "); lastchar←0;
	end;
if italmode=0 then begin print("$"); k←k+1; italmode←1 end;
if basemode=d then basesave←basesave&snexchar
else	begin if basemode≠0 then
		begin if length(basesave)=1 then begin print(basesave);k←k+1 end
			else begin print("{",basesave,"}");k←k+length(basesave)+2 
				end;
		end;
	basesave←snexchar;basemode←d;
	print(temp←d); k←k+1;
	end;
go to mainloop;
end;

procedure italic;
begin integer i,rep;
i←f; italmode←1;
for rep←1 step 1 until 4 do
	begin if i=r then
		begin if(r←r+1)≥100 then r←0;
		char[r]←nchar;
		end;
	if(i←i+1)≥100 then i←0;
	if((char[i]<"A")and(char[i]≠".")and(char[i]≠" "))or(char[i]>"z")
	then output("$");
	end;
italmode←-1; output("{\sl ");
end;

procedure roman;
if italmode>0 then begin italmode←0; print("$"); k←k+1; go to mainloop end
else if italmode=0 then go to mainloop
else begin italmode←0; print("}"); k←k+1; go to mainloop end;

procedure cnewline;
if italmode>0 then begin italmode←0; print("$"); k←k+1; newline end
else if italmode=0 then newline
else begin italmode←0; print("}"); k←k+1; newline end;

procedure dunno;
output("??");

procedure coutput(string s);
begin while nexchar≠"}" do;
output(s);
end;
comment individual processing procedures;
procedure fraction;
begin fracmode←true; italsave←italmode; italmode←1;
if italsave ≤ 0 then output("${")else output("{")
end;

procedure vinculum;
if try("|≡") or try("4") or true then
	begin limmode←-1; output("\sqrt{");
	end;

procedure limit;
if try("|)") then
	begin limmode←-1; output("↓{");
	end
else	begin limmode←+1; output("↑{");
	end;

procedure closeroutine;
begin if fracmode then
	begin fracmode←false; italmode←italsave;
	if italsave ≤ 0 then output("}$") else output("}");
	end;
if limmode>0 then
	begin if try("|)") then limmode←0
	else 	begin
		limmode←-1; output("}↓{");
		end;
	end
else if limmode<0 then limmode←0 else dunno;
output("}");
end;

procedure multijust;
begin mjustmode←true; go to mainloop;
end;

procedure endmj;
begin mjustmode←false; 
k←59; output("\cr ");
end;

procedure mjustquad;
if try(">") then endmj else output("⊗");

procedure slanton;
if exmode then nop else output("{\sl ");

procedure slantoff;
if exmode then nop else output("}");

procedure nonprint;
while true do
	begin while nexchar≠"|" do;
	if nexchar="n" then go to mainloop;
	end;

procedure advance (boolean b);
begin string s; integer x; if b then s←"-"&nexchar else s←nexchar;
while (x←nexchar)≠"}" do s←s&x;
output("\"&s&"skip ");
end;

procedure quadleft;
begin cnewline;
if try("!|9|7") or try("!|9|4|1|1|1") then
	begin print(crlf); lastchar←0; go to mainloop;
	end;
if((lastchar≥"a")and(lastchar≤"z"))or(lastchar=":")or(lastchar=",") then
	begin if try("{A") then coutput("$$");
	output("|qleft ");
	end;
if try("{A3}|≡") or try("{A3}|9|1|≡") or try("{A3}|π|≡") or try("{A3}|9|1|π|≡")
then	begin italmode←0; x←nexchar;
	boldmode←true; print(crlf);
	if x>"9" then output("\algstep "&x);
	if anspgs then output("\ansno "&x);
	exmode←true; output("\exno "&x);
	end;
if try("{A") then
	begin print(crlf,"|qleft "); k←7; advance(false);
	end;
output("|qleft ");
end;

procedure endpage;
begin cnewline;
print(ff);
go to eop;
end;
comment the main control logic;
buf←input(chan,1);if eof then go to quit;
print("%",buf," (C) Addison-Wesley 1978",crlf); buf←0;
lastchar←0;k←0;
fracmode←mjustmode←exmode←boldmode←false;
f←r←0;
italmode←limmode←cfont←0;
mainloop: x←nexchar;
case x of begin "bigcase"
[0] dunno;
["|"] begin y←nexchar; case y of begin "littlecase"
[0] dunno;
['14] endpage;
[","] output("|lower2 ");
["."] output("|raise2 ");
["π"] roman;
["'"] if mjustmode then mjustquad else quadleft;
["T"]["!"] font("s");
["1"]["H"]["→"]nop;
["2"]["3"]["4"]["5"]["6"]["7"]["8"] output(" ");
["9"] if try("|4") and not exmode and not anspgs
then output(" \xskip ") else output(" ");
["∨"] font("r");
["?"] if mjustmode then mjustquad else begin cnewline;output("|qright ")end;
["ε"] if exmode then nop else
	if italmode ≠ 0 then nop else italic;
["("]["f"] fraction;
["d"] if fracmode then 
	begin if try ("2") or try("3") then output("\over ")
	else if try ("5") then output("\atop ") else dunno;
	end else dunno;
[")"] closeroutine;
["≡"] if boldmode then nop else font("=");
[";"] if mjustmode then mjustquad else begin cnewline;output("|qctr ")end;
["¬"] if try("O|4|¬O|4|¬O|4") then output("\cdots ") else output(table1[nexchar]);
[""""] output(table4[nexchar]);
["g"] baseshift("↑");
["J"] if try("#>") then output("|linerule ") else
	if try("!") then output("\eqno ") else dunno;
["∂"] begin mjustmode←true; output("|tab ") end;
["E"] output("|zfa ");
[">"] multijust;
["*"] if try("/") then slanton else dunno;
["\"] slantoff;
["β"] baseshift("↓");
["i"] output("|supsup ");
["h"] nonprint;
[" "] output("\hfill ");
["L"] output("⊗");
["="] if try("|↔6α=↓") then output("≠") else
	if try("|↔$|""o") then output("neqv") else output("|spose ");
["u"] if try("r") or try("c") then limit else
	if try("w") then output("\mathop{") else dunno;
["v"] vinculum;
["r"] output("|infsup ");
["l"] output("|supinf ");
["m"] output("|infinf ");
["≠"] if try("l") then output("↔") else dunno;
["≥"] if try("u") then output("\prod ") else
	if try("C") then output("\emptyset ") else dunno;
["≤"] if try("*/") then output("-") else output(table2[nexchar]);
["↔"] output(table3[nexchar]);
['177] dunno end "littlecase";
	dunno end;

["{"] if try("U0}") then nop else
	if try("H12}({H10}")or try("H11}({H9}") then output("\biglp ") else
	if try("H12}){H10}")or try("H11}){H9}") then output("\bigrp ") else
	if try("IC}") then output("|cancelindent ") else
	if try("I") then
		begin x←nexchar; if try("H") or try(".3H") then
		coutput("|hangindent"&x) else coutput("|indent ");
		end else
	if try("J3}α/↓") then output("*") else
	if try("J3}|≤∩") then output("*") else
	if try("J") then coutput("|raiseordrop ") else
	if try("H") then coutput("|newtype ") else
	if try("A") then advance(false) else
	if try("B") then advance(true) else dunno;
["#"] output("---");
["!"] if try("!") then output("\qquad ") else output("\quad ");
["⊂"]["_"] output("fi");
["∩"]["="] output("ff");
["⊃"]["⊗"] output("fl");
["∪"]["+"] output("ffl");
["∀"]["∃"] output("ffi");
["<"] output("--");
["α"] begin x←nexchar;
if x="o" and try("↓|4αo↓|4αo↓") then output("\cdots ") else
if try("↓") then output(table2[x]) else dunno end;
["]"] begin exmode←false; output("]") end;
["."] begin boldmode←false;
	if try("|4.|4.|4") then output("\ldots ") else output(".") end;
[">"] endmj;
['14] endpage;
["*"] if try("3") then output("!") else if try("#") then nop else dunno;
["-"] if try(">") then output("\-") else output("-");
['177] dunno end "bigcase";
output(x);
comment that ends the mainloop;

eop: end;
comment ending it;
quit: setprint(fn&".out","N");
close(chan);
end;
end